Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
The 'local-pkg' npm package is designed to help developers manage and interact with local packages in a Node.js environment. It provides utilities to check for the existence of local packages, resolve their paths, and require them conditionally.
Check if a local package exists
This feature allows you to check if a specific local package is installed. The function `isPackageExists` returns a boolean indicating the presence of the package.
const { isPackageExists } = require('local-pkg');
if (isPackageExists('some-local-package')) {
console.log('Package exists');
} else {
console.log('Package does not exist');
}
Resolve the path of a local package
This feature helps you resolve the absolute path of a local package. The function `resolvePackagePath` returns the path as a string.
const { resolvePackagePath } = require('local-pkg');
const packagePath = resolvePackagePath('some-local-package');
console.log(`Package path: ${packagePath}`);
Require a local package conditionally
This feature allows you to require a local package conditionally. The function `requireModule` attempts to require the package and returns it if successful, otherwise it returns `null`.
const { requireModule } = require('local-pkg');
const someLocalPackage = requireModule('some-local-package');
if (someLocalPackage) {
console.log('Package loaded successfully');
} else {
console.log('Failed to load package');
}
The 'resolve' package is a library for resolving module paths in Node.js. It provides similar functionality to 'local-pkg' in terms of resolving paths but does not include utilities for checking package existence or conditional requiring.
The 'pkg-up' package finds the closest package.json file by traversing up from a specified directory. While it helps in locating package.json files, it does not offer the same comprehensive utilities for managing local packages as 'local-pkg'.
The 'find-up' package is used to find a file or directory by walking up parent directories. It can be used to locate package.json files but lacks the specific package management utilities provided by 'local-pkg'.
Get information on local packages. Works on both CJS and ESM.
npm i local-pkg
import {
getPackageInfo,
importModule,
isPackageExists,
resolveModule,
} from 'local-pkg'
isPackageExists('local-pkg') // true
isPackageExists('foo') // false
await getPackageInfo('local-pkg')
/* {
* name: "local-pkg",
* version: "0.1.0",
* rootPath: "/path/to/node_modules/local-pkg",
* packageJson: {
* ...
* }
* }
*/
// similar to `require.resolve` but works also in ESM
resolveModule('local-pkg')
// '/path/to/node_modules/local-pkg/dist/index.cjs'
// similar to `await import()` but works also in CJS
const { importModule } = await importModule('local-pkg')
MIT License © 2021 Anthony Fu
FAQs
Get information on local packages.
The npm package local-pkg receives a total of 4,321,640 weekly downloads. As such, local-pkg popularity was classified as popular.
We found that local-pkg demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.